#include "chatwidget.h"
#include "ui_chatwidget.h"

#include <QDateTime>
#include <QDebug>
#include <QIcon>
#include <QMessageBox>

ChatWidget::ChatWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChatWidget)
{
    ui->setupUi(this);

    this->setWindowTitle("Link");
    this->setWindowIcon(QIcon(":/img/res/icon.png"));

    ui->plainTextEdit_input->setFocus();

    // 设置完后自动调用其eventFilter函数
    ui->plainTextEdit_input->installEventFilter(this);
}

ChatWidget::~ChatWidget()
{
    delete ui;
}

bool ChatWidget::eventFilter(QObject *target, QEvent *event)
{
    if(target == ui->plainTextEdit_input)
    {
        if(event->type() == QEvent::KeyPress) // 回车键
        {
            QKeyEvent *k = static_cast<QKeyEvent *>(event);
            if((k->modifiers() == Qt::ControlModifier) && (k->key() == Qt::Key_Return)) {
                ui->plainTextEdit_input->insertPlainText("\n");
                return true;
            }
            if(k->key() == Qt::Key_Return || k->key() == Qt::Key_Enter)
            {
                on_pushButton_send_clicked();
                return true;
            }
        }
    }
    return QWidget::eventFilter(target,event);
}

void ChatWidget::slot_chatReceiveMessage(Message message)
{
    addMessageItem(message);
}

void ChatWidget::on_pushButton_send_clicked()
{
    QString data = ui->plainTextEdit_input->toPlainText();

    if (data.isEmpty()) {
        QMessageBox::information(this, tr("注意"), tr("不能发送空白信息"), QMessageBox::Ok);
        return;
    }

    // 包装成Message
    Message message;
    message.setData(data);

    switch (m_chatType) {
    case Message::SINGAL_CHAT:
        message.setType(Message::Type::SINGAL_CHAT);
        message.setToId(m_toId);
        break;
    case Message::GROUP_CHAT:
        message.setType(Message::Type::GROUP_CHAT);
        message.setGroupId(m_toId);
        break;
    default:
        break;
    }

    // 将要发送的内容显示在listWidget
    addMessageItem(message);
    ui->listWidget_display->scrollToBottom();

//    qDebug() << __FILE__ << __LINE__ << __FUNCTION__ << message.getMessage();
    emit signal_chatSendMessage(message);

    ui->plainTextEdit_input->clear();
}

Message::Type ChatWidget::chatType() const
{
    return m_chatType;
}

void ChatWidget::setChatType(Message::Type newChatType)
{
    m_chatType = newChatType;
}

void ChatWidget::sendOnlineMessage(QString userId)
{
    if (m_chatType == Message::GROUP_CHAT) {
        ui->plainTextEdit_input->setPlainText("用户" + userId +"上线");
        on_pushButton_send_clicked();
    }
}

void ChatWidget::setChatTitle(QString title)
{
    this->setWindowTitle("Link - " + title);
    ui->label_title->setText(title);
}

void ChatWidget::addMessageItem(Message message)
{
    QString name;
    switch (message.type()) {
    case Message::SINGAL_CHAT:
        if (message.toId() == m_toId) {
            name = message.fromId();
        }
        break;
    case Message::GROUP_CHAT:
        name = message.fromId();
        break;
    default:
        break;
    }
    QString str = name +
            message.timestamp().toString(" yy/M/dd hh:mm") +
                  tr("\n") + message.data();
    ui->listWidget_display->addItem(str);
}

QString ChatWidget::toId() const
{
    return m_toId;
}

void ChatWidget::setToId(const QString &newToId)
{
    m_toId = newToId;
}

